﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Xpo;

namespace iAgent
{
    public partial class frmMain : Form, IObserver<string>
    {
        iAgent.Core.IFileHelper helper = DataModel.Core.Settings.Current.IOC.Resolve<iAgent.Core.IFileHelper>();
        public frmMain()
        {
            InitializeComponent();
            //DevExpress.Xpo.UnitOfWork session = new DevExpress.Xpo.UnitOfWork();
            //xpCollection1.Session = session;
            //var pers = new DataModel.Models.Person(session1);
            //var bs = new BindPerson(pers);
            //pers.DriverCard.Serial = "";
            //pers.DriverCard.Number = "";
            //xpCollection1.AddRange(new XPQuery<DataModel.Models.InsurancePolicy>(session1).ToList());   
            helper.Subscribe(this);
            lblCurrentFolder.Text = helper.Path;
        }

        public class BindPerson : DataModel.Models.Person
        {
            DataModel.Models.Person person;
            public BindPerson(DataModel.Models.Person person):base(person.Session)  
            {
                this.person = person;
            }
            [DevExpress.Xpo.DisplayName("ПРОВЕРКА")]
            public override string LastName
            {
                get
                {
                    return person.LastName;
                }
                set
                {
                    person.LastName = value;
                }
            }
            public override string FirstName
            {
                get
                {
                    return person.FirstName;
                }
                set
                {
                    person.FirstName = value;
                }
            }
            public override DateTime BirthDate
            {
                get
                {
                    return person.BirthDate;
                }
                set
                {
                    person.BirthDate = value;
                }
            }
            [DevExpress.Xpo.DisplayName("Номер")]
            public string DriverCardNumber
            {
                get
                {
                    return person.DriverCard.Number;
                }
                set
                {
                    person.DriverCard = new DataModel.Models.DriverCard() { DateOut = person.DriverCard.DateOut, Serial = person.DriverCard.Serial, Number = value };
                }
            }
            public string DriverCardSerial
            {
                get
                {
                    return person.DriverCard.Serial ;
                }
                set
                {
                    person.DriverCard = new DataModel.Models.DriverCard() { DateOut = person.DriverCard.DateOut, Number = person.DriverCard.Number, Serial = value };
                }
            }
            public DateTime DriverCardDateOut
            {
                get
                {
                    return person.DriverCard.DateOut;
                }
                set
                {
                    person.DriverCard = new DataModel.Models.DriverCard() { DateOut = value, Number = person.DriverCard.Number, Serial = person.DriverCard.Serial  };
                }
            }
            public new int Oid
            {
                get
                {
                    return person.Oid;
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string FileName;
            if (helper.AddFile(out FileName))
            {
                listBoxControl1.Items.Add(FileName);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            helper.ChangeRepositoryPath(); 
        }

        private void listBoxControl1_DoubleClick(object sender, EventArgs e)
        {
            if (listBoxControl1.SelectedItem != null)
            {
                string FileName = listBoxControl1.SelectedItem.ToString();
                helper.OpenFile(FileName); 
            }
        }

        public void OnCompleted()
        {
            lblCurrentFolder.Text = String.Format("Папка заменена на {0}", helper.Path);
        }

        public void OnError(Exception error)
        {
            lblCurrentFolder.Text = error.Message;
        }

        public void OnNext(string value)
        {
            lblCurrentFolder.Text = value;
        }
    }
}
